﻿<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background: #1a1a1a;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            color: #fff;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 15px;
        }
        .date-group {
            margin-bottom: 20px;
        }
        .date {
            color: #888;
            font-size: 14px;
            padding: 10px 0;
        }
        .news-item {
            background: #2a2a2a;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px;
            cursor: pointer;
            border: 1px solid #333;
        }
        .news-header {
            display: flex;
        }
        .news-index {
            color: #888;
            margin-right: 10px;
            min-width: 20px;
        }
        .news-content {
            flex: 1;
        }
        .news-main {
            display: flex;
            justify-content: space-between;
        }
        .news-title {
            flex: 1;
            font-size: 16px;
            margin-right: 15px;
            color: #fff;
        }
        .vote-tag {
            display: inline-block;
            background: #1890ff33;
            color: #1890ff;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-top: 5px;
        }
        .hot-info {
            text-align: right;
            min-width: 80px;
        }
        .hot-value {
            color: #ff4d4f;
            font-weight: bold;
        }
        .hot-icons {
            color: #ff4d4f;
            margin-top: 4px;
        }
        .news-detail {
            display: none;
            margin-top: 15px;
            border-top: 1px solid #333;
            padding-top: 15px;
        }
        .news-detail.active {
            display: block;
        }
        .detail-content {
            font-size: 14px;
            line-height: 1.6;
            color: #ccc;
            margin-bottom: 15px;
        }
        .stock-list {
            background: #222;
            border-radius: 4px;
            padding: 10px;
        }
        .stock-list-title {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #fff;
        }
        .stock-item {
            display: flex;
            justify-content: space-between;
            padding: 8px;
            border-bottom: 1px solid #333;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .stock-item:hover {
            background-color: #333;
        }
        .stock-item:last-child {
            border-bottom: none;
        }
        .stock-info {
            display: flex;
            align-items: center;
        }
        .stock-name {
            color: #fff;
            margin-right: 10px;
        }
        .stock-code {
            color: #888;
            font-size: 13px;
        }
        .stock-tag {
            display: inline-block;
            background: #ff4d4f33;
            color: #ff4d4f;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 12px;
            margin-left: 8px;
        }
        .loading {
            text-align: center;
            color: #888;
            padding: 10px;
        }
   ::-webkit-scrollbar {  
            height: 0rem;  
            width: 0px;  
        } 
    </style>
</head>

<body>
    <div class="container" id="newsList"></div>

    <script>
        async function fetchNews() {
            try {
                const response = await fetch('https://applhb.longhuvip.com/w1/api/index.php?Index=0&PhoneOSNew=2&VerSion=5.16.0.3&a=InfoList&apiv=w38&c=Topic&st=15');
                const data = await response.json();
                if(data.errcode === "0") {
                    renderNews(data.List);
                }
            } catch (error) {
                console.error('获取数据失败:', error);
            }
        }

        async function fetchDetail(id) {
            try {
                const response = await fetch(`https://applhb.longhuvip.com/w1/api/index.php?ID=${id}&PhoneOSNew=2&VerSion=5.16.0.3&a=InfoGet&apiv=w38&c=Topic`);
                const data = await response.json();
                return data;
            } catch (error) {
                console.error('获取详情失败:', error);
                return null;
            }
        }

        async function fetchStockList(themeId) {
            try {
                const response = await fetch(`https://applhb.longhuvip.com/w1/api/index.php?ID=${themeId}&PhoneOSNew=2&VerSion=5.16.0.3&a=InfoGet&apiv=w38&c=Theme`);
                const data = await response.json();
                return data;
            } catch (error) {
                console.error('获取股票列表失败:', error);
                return null;
            }
        }

        function formatHotVal(val) {
            if (val >= 10000) {
                return (val / 10000).toFixed(2) + 'w';
            }
            return val;
        }

        function getHotIcons(hotTag) {
            return '🔥'.repeat(hotTag);
        }

        function renderNews(newsGroups) {
            const container = document.getElementById('newsList');
            container.innerHTML = ''; 

            newsGroups.forEach((group, groupIndex) => {
                const dateGroup = document.createElement('div');
                dateGroup.className = 'date-group';
                
                dateGroup.innerHTML = `
                    <div class="date">${group.Day}</div>
                    ${group.List.map((item, index) => `
                        <div class="news-item" data-id="${item.ID}">
                            <div class="news-header">
                                <span class="news-index">${index + 1}</span>
                                <div class="news-content">
                                    <div class="news-main">
                                        <div>
                                            <div class="news-title">${item.Title}</div>
                                        </div>
                                        <div class="hot-info">
                                            <div class="hot-value">${formatHotVal(item.HotVal)}</div>
                                            <div class="hot-icons">${getHotIcons(item.HotTag)}</div>
                                        </div>
                                    </div>
                                    <div class="news-detail">
                                        <div class="loading">加载中...</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    `).join('')}
                `;
                container.appendChild(dateGroup);
            });

            // 添加点击事件监听
            document.querySelectorAll('.news-item').forEach(item => {
                item.addEventListener('click', async function(event) {
                    
                    if (event.target.closest('.stock-item')) {
                        return;
                    }

                    const id = this.dataset.id;
                    const detailDiv = this.querySelector('.news-detail');
                    
                    if (detailDiv.classList.contains('active')) {
                        detailDiv.classList.remove('active');
                        return;
                    }

                    detailDiv.classList.add('active');
                    
                    const detail = await fetchDetail(id);
                    if (detail) {
                        let stockListHTML = '';
                        if (detail.ThemeID) {
                            const stocksData = await fetchStockList(detail.ThemeID);
                            if (stocksData && stocksData.StockList && stocksData.StockList.length > 0) {
                                // 对股票列表按热度值排序
                                const sortedStocks = [...stocksData.StockList].sort((a, b) => {
                                    return parseInt(b.HotNum) - parseInt(a.HotNum);
                                });

                                var stocks ="";

                                for (let i = 0; i < sortedStocks.length; i++) {  
                                    var numberPart = sortedStocks[i].StockID; 
                                    if(numberPart.substring(0, 1) === '0' ||  numberPart.substring(0, 1) === '3')
                                    numberPart='0#'+numberPart;

                                    if(numberPart.substring(0, 1) === '6')
                                    numberPart='1#'+numberPart; 

                                    if(numberPart.substring(0, 1) === '4' ||  numberPart.substring(0, 1) === '8' ||  numberPart.substring(0, 1) === '9')
                                    numberPart='2#'+numberPart; 

                                    stocks+=numberPart; 
                                    stocks += "|"; 
                                } 

                                stocks=stocks.trimEnd("|");

                                stockListHTML = `
                                    <div class="stock-list">
                                        <div class="stock-list-title">相关股票 <a style="color:#FFA07A;" href='http://www.treeid/AddToBlock_${stocks}'>+板块</a></div>  
                                        ${sortedStocks.map(stock => `
                                            <div class="stock-item" onclick="window.location.href='http://www.treeid/code_${stock.StockID}'">
                                                <div class="stock-info">
                                                    <span class="stock-name">${stock.prod_name}</span>
                                                    <span class="stock-code">${stock.StockID}</span>
                                                    ${stock.Tag && stock.Tag.length > 0 ? `
                                                        <span class="stock-tag">${stock.Tag[0].Name}</span>
                                                    ` : ''}
                                                </div>
                                                <div class="hot-value">${stock.HotNum}</div>
                                            </div>
                                        `).join('')}
                                    </div>
                                `;
                            }
                        }

                        detailDiv.innerHTML = `
                            <div class="detail-content">${detail.Content || ''}</div>
                            ${stockListHTML}
                        `;
                    } else {
                        detailDiv.innerHTML = '<div class="loading">获取详情失败</div>';
                    }
                });
            });
        }

        document.addEventListener('DOMContentLoaded', fetchNews);
    </script>
</body>
</html>